package com.ruoyi.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.ExExperimentMain;
import com.ruoyi.domain.ExExperimentSide;
import com.ruoyi.service.IExExperimentSideService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生实验子Controller
 * 
 * @author ruoyi
 * @date 2024-09-21
 */
@RestController
@RequestMapping("/system/side")
public class ExExperimentSideController extends BaseController
{
    @Autowired
    private IExExperimentSideService exExperimentSideService;

    /**
     * 查询学生实验子列表
     */
    @PreAuthorize("@ss.hasPermi('system:side:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExExperimentSide exExperimentSide)
    {
        startPage();
        List<ExExperimentSide> list = exExperimentSideService.selectExExperimentSideList(exExperimentSide);
        return getDataTable(list);
    }

    /**
     * 导出学生实验子列表
     */
    @PreAuthorize("@ss.hasPermi('system:side:export')")
    @Log(title = "学生实验子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExExperimentSide exExperimentSide)
    {
        List<ExExperimentSide> list = exExperimentSideService.selectExExperimentSideList(exExperimentSide);
        ExcelUtil<ExExperimentSide> util = new ExcelUtil<ExExperimentSide>(ExExperimentSide.class);
        util.exportExcel(response, list, "学生实验子数据");
    }

    /**
     * 获取学生实验子详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:side:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(exExperimentSideService.selectExExperimentSideById(id));
    }

    /**
     * 完成步骤
     */
    @PostMapping("/compStep")
    public AjaxResult compStep(@RequestBody ExExperimentSide exExperimentSide)
    {
        ExExperimentSide experimentSide = exExperimentSideService.selectExExperimentSideById(exExperimentSide.getId());
        experimentSide.setPictureUrl(exExperimentSide.getFileList().stream().map(String::valueOf).collect(Collectors.joining(",")));
        experimentSide.setStepSummary(exExperimentSide.getStepSummary());
        experimentSide.setStepStatus("2");
        Date date = new Date();
        experimentSide.setStepEndTime(date);
        exExperimentSideService.updateExExperimentSide(experimentSide);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(date);
        return success(dateStr);
    }

    /**
     * 新增学生实验子
     */
    @PreAuthorize("@ss.hasPermi('system:side:add')")
    @Log(title = "学生实验子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExExperimentSide exExperimentSide)
    {
        return toAjax(exExperimentSideService.insertExExperimentSide(exExperimentSide));
    }

    /**
     * 修改学生实验子
     */
    @PreAuthorize("@ss.hasPermi('system:side:edit')")
    @Log(title = "学生实验子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExExperimentSide exExperimentSide)
    {
        return toAjax(exExperimentSideService.updateExExperimentSide(exExperimentSide));
    }

    /**
     * 删除学生实验子
     */
    @PreAuthorize("@ss.hasPermi('system:side:remove')")
    @Log(title = "学生实验子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(exExperimentSideService.deleteExExperimentSideByIds(ids));
    }
}
